package com.xuan.bigdog.lib.location.activity;

import java.util.ArrayList;
import java.util.List;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;

import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.BaiduMapOptions;
import com.baidu.mapapi.map.BitmapDescriptorFactory;
import com.baidu.mapapi.map.MapStatus;
import com.baidu.mapapi.map.MapView;
import com.baidu.mapapi.map.MarkerOptions;
import com.baidu.mapapi.model.LatLng;
import com.xuan.bigapple.lib.ioc.app.BPActivity;
import com.xuan.bigapple.lib.utils.Validators;
import com.xuan.bigdog.R;
import com.xuan.bigdog.lib.location.DGPoint;

/**
 * 基础地图
 * 
 * @author xuan
 */
public class DGMapActivity extends BPActivity {
	private Bitmap defaultLocationBitmap;// 默认当前图片坐标

	private MapView mMapView;
	private BaiduMap mBaiduMap;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		defaultLocationBitmap = BitmapFactory.decodeResource(getResources(),
				R.drawable.dg_location_default_loc_mark);
		// 设置展示中心点
		initBaiduMapAndCenterPoint();
		// 设置View到界面
		initContentView();
		// 设置需要标记的点
		initMarkLocation();
	}

	// 初始化view
	private void initContentView() {
		View titleView = configTitleView();
		if (null != titleView) {
			LinearLayout ll = (LinearLayout) LayoutInflater.from(this).inflate(
					R.layout.dg_location_map, null);
			ll.addView(titleView);
			ll.addView(mMapView);
			setContentView(ll);
		} else {
			setContentView(mMapView);
		}
	}

	/** 设置展示中心点 */
	private void initBaiduMapAndCenterPoint() {
		DGPoint centerPoint = configCenterPoint();
		if (null != centerPoint) {
			LatLng p = new LatLng(centerPoint.x, centerPoint.y);
			mMapView = new MapView(this,
					new BaiduMapOptions().mapStatus(new MapStatus.Builder()
							.target(p).build()));
		} else {
			mMapView = new MapView(this, new BaiduMapOptions());
		}
		mBaiduMap = mMapView.getMap();
	}

	/** 设置需要标记的点 */
	private void initMarkLocation() {
		List<DGPoint> locationPointList = configLocationPointList();
		List<Bitmap> locationPointBitmapList = configLocationPointBitmapList();
		if (Validators.isEmpty(locationPointBitmapList)) {
			// 如果标记图片是空，设置默认图片
			locationPointBitmapList = new ArrayList<Bitmap>();
			for (int i = 0, n = locationPointList.size(); i < n; i++) {
				locationPointBitmapList.add(defaultLocationBitmap);
			}
		}

		if (!Validators.isEmpty(locationPointList)) {
			for (int i = 0, n = locationPointList.size(); i < n; i++) {
				// 构建MarkerOption，用于在地图上添加Marker
				DGPoint p = locationPointList.get(i);

				MarkerOptions option = new MarkerOptions();
				option.position(new LatLng(p.x, p.y));
				option.icon(BitmapDescriptorFactory
						.fromBitmap(defaultLocationBitmap));

				// 在地图上添加Marker，并显示
				mBaiduMap.addOverlay(option);
			}
		}
	}

	@Override
	protected void onPause() {
		super.onPause();
		mMapView.onPause();
	}

	@Override
	protected void onResume() {
		super.onResume();
		mMapView.onResume();
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		mMapView.onDestroy();
	}

	/**
	 * 子类复写，设置标题栏
	 * 
	 * @return
	 */
	protected View configTitleView() {
		return null;
	}

	/**
	 * 子类复写，显示地图中心点
	 * 
	 * @return
	 */
	protected DGPoint configCenterPoint() {
		return null;
	}

	/**
	 * 子类复写，当前位置默认图片
	 * 
	 * @return
	 */
	protected List<Bitmap> configLocationPointBitmapList() {
		return null;
	}

	/**
	 * 子类复写，需要标记的位置信息
	 * 
	 * @return
	 */
	protected List<DGPoint> configLocationPointList() {
		return null;
	}

}
